package com.cddx.logistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cddx.logistics.domain.dto.plan.PlanCable2PageDTO;
import com.cddx.logistics.domain.dto.plan.PlanNewPageDTO;
import com.cddx.logistics.domain.dto.plan.PlanNormalPageDTO;
import com.cddx.logistics.domain.dto.plan.PlanSparePageDTO;
import com.cddx.logistics.domain.pojo.plan.PlanCable2;
import com.cddx.logistics.domain.pojo.plan.PlanNew;
import com.cddx.logistics.domain.pojo.plan.PlanNormal;
import com.cddx.logistics.domain.pojo.plan.PlanSpare;
import com.cddx.logistics.domain.vo.SelectObject;
import com.cddx.logistics.mapper.PlanMapper;
import com.cddx.logistics.service.PlanService;
import com.cddx.logistics.util.model.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service

public class PlanServiceImpl implements PlanService {
    @Resource
    private PlanMapper planMapper;
    @Override
    public R listNormalPage(PlanNormalPageDTO planNormalPageDTO) {
        PageHelper.startPage(planNormalPageDTO.getPage(), planNormalPageDTO.getLimit());
        List<PlanNormal> planNormalList = planMapper.listNormalPage(planNormalPageDTO);
        PageInfo<PlanNormal> pageInfo = new PageInfo<>(planNormalList);
        return R.ok(pageInfo);
    }

    @Override
    public R deleteNormal(List<Integer> ids) {
        return planMapper.deleteNormal(ids)? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R addNormal(PlanNormal planNormal) {
        return planMapper.insertNormal(planNormal)? R.ok(planNormal) : R.error("添加失败");
    }

    @Override
    public R updateNormal(PlanNormal planNormal) {
        PlanNormal po=planMapper.selectNormalById(planNormal.getId());
        BeanUtil.copyProperties(planNormal,po);
        return planMapper.updateNormal(po)? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R deleteNormal(Integer id) {
        return planMapper.deleteNormalById(id)? R.ok("删除成功") : R.error("删除失败");
    }
/*
* 备品计划相关*/
    @Override
    public R listSparePage(PlanSparePageDTO planSparePageDTO) {
        PageHelper.startPage(planSparePageDTO.getPage(), planSparePageDTO.getLimit());
        List<PlanSpare> planSpareList = planMapper.listSparePage(planSparePageDTO);
        PageInfo<PlanSpare> pageInfo = new PageInfo<>(planSpareList);
        return R.ok(pageInfo);
    }

    @Override
    public R addSpare(PlanSpare planSpare) {
        return planMapper.insertSpare(planSpare) ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R updateSpare(PlanSpare planSpare) {
        PlanSpare po = planMapper.selectSpareById(planSpare.getId());
        if (po == null) {
            return R.error("数据不存在");
        }
        BeanUtil.copyProperties(planSpare, po);
        return planMapper.updateSpare(po) ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R deleteSpare(List<Integer> ids) {
        return planMapper.deleteSpare(ids) ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R deleteSpare(Integer id) {
        return planMapper.deleteSpareById(id) ? R.ok("删除成功") : R.error("删除失败");
    }
    /*
    * 新品/临措计划*/
    @Override
    public R listNewPage(PlanNewPageDTO planNewPageDTO) {
        PageHelper.startPage(planNewPageDTO.getPage(), planNewPageDTO.getLimit());
        List<PlanNew> planNewList = planMapper.listNewPage(planNewPageDTO);
        PageInfo<PlanNew> pageInfo = new PageInfo<>(planNewList);
        return R.ok(pageInfo);
    }

    @Override
    public R addNew(PlanNew planNew) {
        return planMapper.insertNew(planNew) ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R updateNew(PlanNew planNew) {
        PlanNew po = planMapper.selectNewById(planNew.getId());
        if (po == null) {
            return R.error("数据不存在");
        }
        BeanUtil.copyProperties(planNew, po);
        return planMapper.updateNew(po) ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R deleteNew(List<Integer> ids) {
        return planMapper.deleteNew(ids) ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R deleteNew(Integer id) {
        return planMapper.deleteNewById(id) ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R listCable2Page(PlanCable2PageDTO planCable2PageDTO) {
        PageHelper.startPage(planCable2PageDTO.getPage(), planCable2PageDTO.getLimit());
        List<PlanCable2> list = planMapper.listCable2Page(planCable2PageDTO);
        PageInfo<PlanCable2> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    @Override
    public R addCable2(PlanCable2 planCable2) {
        return planMapper.insertCable2(planCable2) ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R updateCable2(PlanCable2 planCable2) {
        PlanCable2 po = planMapper.selectCable2ById(planCable2.getId());
        if (po == null) {
            return R.error("数据不存在");
        }
        BeanUtil.copyProperties(planCable2, po);
        return planMapper.updateCable2(po) ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R deleteCable2(List<Integer> ids) {
        return planMapper.deleteCable2(ids) ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R deleteCable2(Integer id) {
        return planMapper.deleteCable2ById(id) ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    @Transactional
    public R batchAddNormal(List<PlanNormal> planNormals) {
        boolean result = planMapper.insertBatchNormal(planNormals);
        return result ? R.ok("批量导入成功") : R.error("批量导入失败");
    }

    @Override
    public List<SelectObject> queryAll() {
        List<SelectObject> selectObjects = planMapper.queryAll();
        return new ArrayList<>(selectObjects.stream()
                .collect(Collectors.toMap(
                        SelectObject::getName,   // 以 name 作为 key
                        so -> so,               // value 是对象本身
                        (existing, replacement) -> existing // 如果 key 冲突，保留第一个
                ))
                .values());
    }
}
